package com.aia.ilp.web.admin;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.jxls.transformer.XLSTransformer;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;

import com.aia.ann.domain.AgentExample;
import com.aia.ann.service.ChiefService;
import com.cfh.ahead.Constant;
import com.cfh.util.Tools;
import com.opensymphony.xwork2.ActionSupport;

public class ReportBinCustAction extends ActionSupport {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ReportBinCustAction.class);

	/**
	 * Logger for this class
	 */

	private static final long serialVersionUID = 1L;
	private String DATE_BEGIN;
	private String DATE_END;
	private ChiefService chiefService;

	public String execute() throws Exception {
		if (logger.isDebugEnabled()) {
			logger.debug("execute() - start"); //$NON-NLS-1$
		}

		HttpServletResponse res = ServletActionContext.getResponse();
		res.setContentType("application/vnd.ms-excel");
		res.setHeader("Content-Disposition", "attachment; filename=\"report_" + System.currentTimeMillis() + ".xls\"");

		Map beans = new HashMap();
		beans.put("PRINT_DATE", Tools.getToday());

		AgentExample example = new AgentExample();
		example.createCriteria().andAG_DATEBetween(DATE_BEGIN, DATE_END).andAG_FLAG_UPLOADEqualTo(Constant.Y);
		example.setOrderByClause("AG_DATE DESC,AG_TIME DESC");
		List list = chiefService.selectAgentByExample(example);
		beans.put("list", list);
		XLSTransformer transformer = new XLSTransformer();
		InputStream in = this.getClass().getResourceAsStream("/excel/" + this.getClass().getSimpleName() + ".xls");
		HSSFWorkbook workbook = transformer.transformXLS(in, beans);
		try {
			workbook.write(res.getOutputStream());
		} catch (IOException e) {
			logger.error("report error:" + e);
		}

		if (logger.isDebugEnabled()) {
			logger.debug("execute() - end"); //$NON-NLS-1$
		}
		return null;
	}

	public void setDATE_BEGIN(String dATE_BEGIN) {
		DATE_BEGIN = dATE_BEGIN;
	}

	public String getDATE_BEGIN() {
		return DATE_BEGIN;
	}

	public void setDATE_END(String dATE_END) {
		DATE_END = dATE_END;
	}

	public String getDATE_END() {
		return DATE_END;
	}

	public void setChiefService(ChiefService chiefService) {
		this.chiefService = chiefService;
	}

	public ChiefService getChiefService() {
		return chiefService;
	}

}
